//
//  DiscoverShopViewController.m
//  陶瓷
//
//  Created by 郭天龙 on 15/7/29.
//  Copyright (c) 2015年 王戈. All rights reserved.
//

#import "DiscoverShopViewController.h"
#import "TCNetworkTool.h"
#import "DisShopTableViewCell.h"
#import "DisShopSecondTableViewCell.h"
#import "DisShopThridTableViewCell.h"
#import "TCNavigationItemFavouriteBtn.h"
//#import "TailDetailTableViewCell.h"
#import "STImageSkimController.h"
#import "NSDictionary+TCTurnToURLArray.h"
#import "HomeScrollHeaderView.h"

@interface DiscoverShopViewController ()<HomeScrollHeaderViewDelegate>

@property (nonatomic, weak) IBOutlet UITableView *tableview;
@property (nonatomic, strong) NSDictionary  *datadic;

@property (nonatomic, strong) UIButton *topBtn;
@property (nonatomic, strong) UIButton *favorBtn;

@property (nonatomic, strong) UIButton *cover;

@property (nonatomic, strong) NSMutableArray *picArray;
@property (nonatomic ,strong) NSMutableArray *picArrayUrl;

//@property (nonatomic, strong) BMKMapView *mapView;

@property (strong,nonatomic) BMKLocationService *locService;

@property (nonatomic, assign) CGFloat locaLatitude;
@property (nonatomic, assign) CGFloat locaLongitude;

@end

@implementation DiscoverShopViewController

#pragma mark - Action
- (IBAction)clickFindCompany:(id)sender
{
    //    TailMapViewController *mapvc = [[TailMapViewController alloc] init];
    //    [self.navigationController pushViewController:mapvc animated:YES];
    
    [self initBMKUserLocation];
}

- (IBAction)clickTel:(id)sender
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",[self.datadic objectForKey:@"ContactNumber"]]]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.picArray = [[NSMutableArray alloc] initWithCapacity:6];
    self.picArrayUrl = [[NSMutableArray alloc] initWithCapacity:6];
    self.tableview.tableFooterView = [UIView new];
    self.tableview.backgroundColor = [UIColor colorWithString:@"#EFEFF4"];
    self.tableview.hidden = YES;
    [self.tableview setSeparatorInset:UIEdgeInsetsZero];
    if ([self.tableview respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableview setLayoutMargins:UIEdgeInsetsZero];
    }
    
    [self.tableview registerNib:[UINib nibWithNibName:@"DisShopTableViewCell" bundle:nil] forCellReuseIdentifier:@"DisShopTableViewCell"];
    [self.tableview registerNib:[UINib nibWithNibName:@"DisShopSecondTableViewCell" bundle:nil] forCellReuseIdentifier:@"DisShopSecondTableViewCell"];
    [self.tableview registerNib:[UINib nibWithNibName:@"DisShopThridTableViewCell" bundle:nil] forCellReuseIdentifier:@"DisShopThridTableViewCell"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(infoChange:) name:@"EditSuccessNotification" object:nil];
    
    [SVProgressHUD showWithStatus:@"正在获取商铺详情 请稍后.."];
    [self.view addSubview:self.cover];
    TCNetworkTool *tctool = [TCNetworkTool sharedNetTool];
    [tctool rentDetailWithID:self.sid andSuccessBlocks:^(NSDictionary *resultDic) {
        [SVProgressHUD dismiss];
        [self.cover removeFromSuperview];
        int index = 0;
        if (![resultDic[@"Img1"] isEqual:[NSNull null]]) {
            [_picArray addObject:@{@"ImageUrl":[resultDic objectForKey:@"Img1"],@"index":[NSNumber numberWithInt:index]}];
            [_picArrayUrl addObject:[resultDic objectForKey:@"Img1"]];
            index++;
        }
        if (![resultDic[@"Img2"] isEqual:[NSNull null]]) {
            [_picArray addObject:@{@"ImageUrl":[resultDic objectForKey:@"Img2"],@"index":[NSNumber numberWithInt:index]}];
            [_picArrayUrl addObject:[resultDic objectForKey:@"Img2"]];
            index++;
        }
        if (![resultDic[@"Img3"] isEqual:[NSNull null]]) {
            [_picArray addObject:@{@"ImageUrl":[resultDic objectForKey:@"Img3"],@"index":[NSNumber numberWithInt:index]}];
            [_picArrayUrl addObject:[resultDic objectForKey:@"Img3"]];
            index++;
        }
        if (![resultDic[@"Img4"] isEqual:[NSNull null]]) {
            [_picArray addObject:@{@"ImageUrl":[resultDic objectForKey:@"Img4"],@"index":[NSNumber numberWithInt:index]}];
            [_picArrayUrl addObject:[resultDic objectForKey:@"Img4"]];
            index++;
        }
        if (![resultDic[@"Img5"] isEqual:[NSNull null]]) {
            [_picArray addObject:@{@"ImageUrl":[resultDic objectForKey:@"Img5"],@"index":[NSNumber numberWithInt:index]}];
            [_picArrayUrl addObject:[resultDic objectForKey:@"Img5"]];
            index++;
        }
        if (![resultDic[@"Img6"] isEqual:[NSNull null]]) {
            [_picArray addObject:@{@"ImageUrl":[resultDic objectForKey:@"Img6"],@"index":[NSNumber numberWithInt:index]}];
            [_picArrayUrl addObject:[resultDic objectForKey:@"Img6"]];
            index++;
        }
        
        [self setupHeaderViewArray:_picArray];
        if (resultDic) {
            self.datadic = [resultDic mutableCopy];
            [self.tableview reloadData];
            self.tableview.hidden = NO;
            [self settingDetails];
        }
    } andFailureBlocks:^(NSString *failureMessage) {
        [SVProgressHUD dismiss];
        [self.cover removeFromSuperview];
        [MBHUDHelper showWarningWithText:failureMessage];
    }];
    [self setupNavigationItem];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationItem setTitle:@"招租信息"];
    if(!IsLoginIn) {
        _favorBtn.hidden = YES;
    } else {
        if (!_isFavor && _favorBtn) {
            [[TCNetworkTool sharedNetTool] checkIsFavouritesWithType:kTCCollectTypeRecruitment andObjectID:_sid andSuccessBlocks:^(NSString *successMessage) {
                self.isFavor = [successMessage isEqualToString:@"已关注此信息"];
            } andFailureBlocks:^(NSString *failureMessage) {
                if ([failureMessage isEqualToString:@"未关注此信息"]) {
                    self.isFavor = NO;
                }
            }];
        } else {
            self.favorBtn.selected = YES;
        }
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationItem setTitle:@""];
    [self.cover removeFromSuperview];
    [SVProgressHUD dismiss];
}

- (UIButton *)cover {
    if (_cover == nil) {
        _cover = [[UIButton alloc] initWithFrame:self.view.bounds];
        _cover.backgroundColor = [UIColor clearColor];
    }
    return _cover;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.section == 0) {
        DisShopSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DisShopSecondTableViewCell"];
        cell = [[NSBundle mainBundle] loadNibNamed:@"DisShopSecondTableViewCell" owner:nil options:nil].firstObject;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setModel:self.datadic];
        
        return cell;
//    }
//    else
//    {
//        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mapcell" ];
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mapcell"];
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        [cell.contentView addSubview:_mapView];
//        return cell;
//    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIImageView *imagev = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 5, 30)];
    imagev.backgroundColor = [UIColor colorWithString:@"#3CD1D4"];
    
    UILabel *browser = [[UILabel alloc] initWithFrame:CGRectMake(kDefaultWindowWidth-120, 10, 120, 10)];
    browser.text = [NSString stringWithFormat:@"已有%@人浏览",[self.datadic objectForKey:@"BrowseNumber"]];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kDefaultWindowWidth, 30)];
    view.backgroundColor = [UIColor colorWithString:@"#EFEFF4"];
    UILabel *headerTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, kDefaultWindowWidth-140, 10)];
    headerTitle.text = [self.datadic objectForKey:@"Title"];
//    headerTitle.text = @"招租信息";
    // 省略号写在句尾
    headerTitle.lineBreakMode = NSLineBreakByTruncatingTail;
    headerTitle.textColor = [UIColor blackColor];
    [headerTitle setFont:[UIFont boldSystemFontOfSize:17]];
    [view addSubview:headerTitle];
    [view addSubview:imagev];
    [view addSubview:browser];
    return view;
}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    style.lineBreakMode = NSLineBreakByWordWrapping;
    
    NSString *temAddr = [self.datadic objectForKey:@"Address"];
    CGFloat heightAddr = 0;
    if (temAddr) {
        CGRect lblSize = [temAddr boundingRectWithSize:CGSizeMake(kDefaultWindowWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17], NSParagraphStyleAttributeName:style} context:nil];
        
        heightAddr = lblSize.size.height;
    }
    
    NSString *tmp = [self.datadic objectForKey:@"Description"];
    CGFloat height = 0;
    if (tmp) {
        CGRect lblSize = [tmp boundingRectWithSize:CGSizeMake(kDefaultWindowWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17], NSParagraphStyleAttributeName:style} context:nil];
        
        height = lblSize.size.height;
    }
    return 260+height+heightAddr;
}
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return 3;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    return 1;
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    switch (indexPath.section) {
//        case 0:
//        {
//            DisShopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DisShopTableViewCell"];
//            cell = [[NSBundle mainBundle] loadNibNamed:@"DisShopTableViewCell" owner:nil options:nil].firstObject;
//            cell.selectionStyle = UITableViewCellSelectionStyleNone;
//            [cell setModel:self.datadic];
//            return cell;
//        }
//            break;
//        case 1:
//        {
//            DisShopSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DisShopSecondTableViewCell"];
//            cell = [[NSBundle mainBundle] loadNibNamed:@"DisShopSecondTableViewCell" owner:nil options:nil].firstObject;
//            cell.selectionStyle = UITableViewCellSelectionStyleNone;
//            [cell setModel:self.datadic];
//            return cell;
//        }
//            break;
//        case 2:
//        {
//            DisShopThridTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DisShopThridTableViewCell"];
//            cell = [[NSBundle mainBundle] loadNibNamed:@"DisShopThridTableViewCell" owner:nil options:nil].firstObject;
//            cell.selectionStyle = UITableViewCellSelectionStyleNone;
//            [cell setModel:self.datadic];
//            return cell;
//        }
//            break;
//            
//        default:
//        {
//            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"disjob"];
//            
//            cell = [[UITableViewCell alloc] init];
//            return cell;
//        }
//            break;
//    }
//    
//}
//
//#pragma mark - UITableViewDelegate
//-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
//        [cell setSeparatorInset:UIEdgeInsetsZero];
//    }
//    
//    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
//        [cell setLayoutMargins:UIEdgeInsetsZero];
//    }
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    switch (indexPath.section) {
//        case 0:
//            return 140;
//            break;
//        case 1:
//            return 200;
//            break;
//        case 2:
//            return 160;
//            break;
//        default:
//            return 0;
//            break;
//    }
//}
//
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    
//}

- (void)setupHeaderViewArray:(NSArray *)array
{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kDefaultWindowWidth, 200)];
    header.backgroundColor = [UIColor whiteColor];
//    UIImageView *titleIcon = [[UIImageView alloc] initWithFrame:CGRectMake(kDefaultWindowWidth*0.5-80, 15, 160, 160)];
//    titleIcon.backgroundColor = [UIColor lightGrayColor];
//    [titleIcon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",uri]] placeholderImage:nil];
//    
//    UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(kDefaultWindowWidth*0.5+60, 150, 40, 20)];
//    numberLabel.text = [NSString stringWithFormat:@"(%lu)",(unsigned long)_picArray.count];
//    [numberLabel setTextColor:[UIColor whiteColor]];
//    
//    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickHeaderPic)];
//    gesture.numberOfTapsRequired = 1;
//    [header addGestureRecognizer:gesture];
//    [header addSubview:titleIcon];
//    [header addSubview:numberLabel];
    HomeScrollHeaderView *headview = [[HomeScrollHeaderView alloc] initWithFrame:CGRectMake(kDefaultWindowWidth*0.5-80, 15, 160, 160)];
    headview.delegate = self;
    headview.images =  array;
    headview.scrollTime = 3.0f;
    [headview loadLoopScrollView];
    [header addSubview:headview];
    self.tableview.tableHeaderView = header;
    self.tableview.tableFooterView = [UIView new];
    
}

- (void)didAdvertiseButton:(id)dic
{
    
    STImageSkimController *skimController = [[STImageSkimController alloc] initWithImages:_picArrayUrl andIndex:[[dic objectForKey:@"index"] intValue]];
    
    [self presentViewController:skimController animated:YES completion:nil];
    
}

- (void)clickHeaderPic
{
    STImageSkimController *skimController = [[STImageSkimController alloc] initWithImages:_picArrayUrl andIndex:0];
    
    [self presentViewController:skimController animated:YES completion:nil];
}

#pragma mark - BMKLocationServiceDelegate
/**
 *  初始化百度地图用户位置管理类
 */
- (void)initBMKUserLocation
{
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    [self startLocation];
}

/**
 *  打开定位服务
 */
-(void)startLocation
{
    [_locService startUserLocationService];
}

/**
 *  关闭定位服务
 */
-(void)stopLocation
{
    [_locService stopUserLocationService];
}


//-(void)mapView:(BMKMapView *)mapView didUpdateUserLocation:(BMKUserLocation *)userLocation{
//    NSLog(@"latitude--%f,longtitude---%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
//    self.locaLatitude=userLocation.location.coordinate.latitude;//纬度
//    self.locaLongitude=userLocation.location.coordinate.longitude;//精度
//}

/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation;
{
    self.locaLatitude = userLocation.location.coordinate.latitude;
    self.locaLongitude = userLocation.location.coordinate.longitude;
    [self stopLocation];//(如果需要实时定位不用停止定位服务)
    [self mapViewWillStartLocatingUser:nil];
}

-(void)mapViewWillStartLocatingUser:(BMKMapView *)mapView{
    NSLog(@"开始定位");
    BMKOpenTransitRouteOption *opt = [[BMKOpenTransitRouteOption alloc] init];
    opt.appScheme = @"baidumapsdk://mapsdk.baidu.com";//用于调起成功后，返回原应用
    //初始化起点节点
    BMKPlanNode* start = [[BMKPlanNode alloc]init];
    //指定起点经纬度
    CLLocationCoordinate2D coor1;
    coor1.latitude = self.locaLatitude;
    coor1.longitude = self.locaLongitude;
    //指定起点名称
    start.name = @"我的位置";
    start.pt = coor1;
    //指定起点
    opt.startPoint = start;
    
    //初始化终点节点
    BMKPlanNode* end = [[BMKPlanNode alloc]init];
    CLLocationCoordinate2D coor2;
    coor2.latitude = [[self.datadic objectForKey:@"Latitude"] floatValue];
    coor2.longitude = [[self.datadic objectForKey:@"Longitude"] floatValue];
    end.pt = coor2;
    //指定终点名称
    end.name = [self.datadic objectForKey:@"CompanyAddress"];
    opt.endPoint = end;
    
    //打开地图公交路线检索
    BMKOpenErrorCode code = [BMKOpenRoute openBaiduMapTransitRoute:opt];
}

- (void)setupNavigationItem
{
    if (_mine) {
        UIButton *topBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        topBtn.frame = CGRectMake(0, 0, 50,30);
        [topBtn setTitle:@"已置顶" forState:UIControlStateNormal];
        [topBtn addTarget:self action:@selector(top) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:topBtn];
        _topBtn = topBtn;
        
//        UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeSystem];
//        editBtn.frame = CGRectMake(0, 0, 30,30);
//        [editBtn setTitle:@"编辑" forState:UIControlStateNormal];
//        [editBtn addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];
//        UIBarButtonItem *rightButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:editBtn];
        
        self.navigationItem.rightBarButtonItems = @[rightButtonItem];
    } else {
        _favorBtn = [TCNavigationItemFavouriteBtn favouriteBtn];
        [_favorBtn addTarget:self action:@selector(favourite) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_favorBtn];
        self.navigationItem.rightBarButtonItem = rightButtonItem;
    }
    
}

// 收藏
- (void)favourite {
    [self.view addSubview:self.cover];
    [[TCNetworkTool sharedNetTool] addFavouritesWithId:nil andUserId:nil andCollectType:kTCCollectTypeRent andTypeID:_sid andAddTime:nil andSuccessBlocks:^(NSString *successMessage) {
        [self.cover removeFromSuperview];
        self.isFavor = [successMessage isEqualToString:@"关注成功，可在我的关注中查看"];
    } andFailureBlocks:^(NSString *failureMessage) {
        [self.cover removeFromSuperview];
    }];
    
}

- (void)setIsFavor:(BOOL)isFavor {
    _isFavor = isFavor;
    _favorBtn.selected = isFavor;
}


- (void)top {
    [SVProgressHUD showWithStatus:@"正在提交指定数据 请稍后.."];
    [self.view addSubview:self.cover];
    [[TCNetworkTool sharedNetTool] recruitmentTopWithID:self.sid andSuccessBlocks:^(NSString *successMessage) {
        [SVProgressHUD dismiss];
        [self.cover removeFromSuperview];
        [SVProgressHUD showSuccessWithStatus:@"置顶成功!"];
        [_topBtn setTitle:@"已置顶" forState:UIControlStateNormal];
        _topBtn.userInteractionEnabled = NO;
    } andFailureBlocks:^(NSString *failureMessage) {
        [MBHUDHelper showWarningWithText:failureMessage];
        [SVProgressHUD dismiss];
        [self.cover removeFromSuperview];
    }];
}

- (void)edit {
    AddMessageController *addMessage = [[AddMessageController alloc] init];
    addMessage.questionArray = rentQuestionArray;
    addMessage.sid = self.datadic[@"Id"];
    addMessage.title = @"商铺信息发布";
    NSArray *imagesArray = [self.datadic turnToURLArray];
    addMessage.answerDict = [@{
                               @"1" : self.datadic[@"Title"] ? self.datadic[@"Title"] : @"",
                               @"2" : self.datadic[@"Price"] ? self.datadic[@"Price"] : @"",
                               @"3" : self.datadic[@"Area"] ? self.datadic[@"Area"] : @"",
                               @"4" : self.datadic[@"Histroy"] ? self.datadic[@"Histroy"] : @"",
                               @"5" : self.datadic[@"Address"] ? self.datadic[@"Address"] : @"",
                               @"6" : self.datadic[@"Description"] ? self.datadic[@"Description"] : @"",
                               @"7" : self.datadic[@"Type"] ? self.datadic[@"Type"] : @"",
                               @"0" : imagesArray
                               } mutableCopy];
    [self.navigationController pushViewController:addMessage animated:YES];
}

- (void)settingDetails {
    [self.tableview reloadData];
//    self.title = self.datadic[@"Title"];
    
    BOOL isTop = [self.datadic[@"IsTop"] isEqual:@1];
    NSString *topString = isTop ? @"已置顶" : @"置顶";
    _topBtn.userInteractionEnabled = !isTop;
    [_topBtn setTitle:topString forState:UIControlStateNormal];
}

- (void)infoChange:(NSNotification *)notification {
    [self.datadic setValuesForKeysWithDictionary:notification.object];
    [self settingDetails];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
